package com.woniuxy.light_smart.controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.light_smart.dto.ApplyDto;
import com.woniuxy.light_smart.dto.LoginDto;
import com.woniuxy.light_smart.entity.Role;
import com.woniuxy.light_smart.entity.User;
import com.woniuxy.light_smart.service.MenuServer;
import com.woniuxy.light_smart.service.RoleService;
import com.woniuxy.light_smart.dto.UpdatePwdDto;
import com.woniuxy.light_smart.dto.UserDto;
import com.woniuxy.light_smart.entity.User;
import com.woniuxy.light_smart.service.MenuServer;
import com.woniuxy.light_smart.service.UserRoleService;
import com.woniuxy.light_smart.service.UserService;
import com.woniuxy.light_smart.util.Result;
import com.woniuxy.light_smart.vo.MenuVo;
import com.woniuxy.light_smart.vo.UserVo;
import com.woniuxy.light_smart.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuServer menuServer;
    @Autowired
    private UserRoleService userRoleService;
    @PostMapping("login")
    public Result login(@Validated(LoginDto.loginGroups.class) @RequestBody LoginDto loginDto){
        User user = userService.selectByTelAndPsw(loginDto);
        if(user!=null){
            List<MenuVo> menuVos = menuServer.selectByTelOrMail(loginDto.getTel(), 0);
            User user1 = userService.selectByTelOrMail(loginDto.getTel());
            return Result.ok().data("menu",menuVos).data("user",user1);
        }
        return Result.error();
    }

//查询
    @GetMapping("")
    public Result sell() {
        List<User> userList = userService.selectAll();
        if (userService != null) {
            return Result.ok().data("list", userList);
        } else {
            return Result.error();
        }
    }
    @PostMapping("apply")
    public Result apply(@RequestBody ApplyDto a){
        int i = userService.applyUser(a);
        return i==1?Result.ok():Result.error();
    }
    @PostMapping("query")
    public Result selAllByLike(@RequestBody UserDto userDto){
        PageHelper.startPage(userDto.getCurrentPage(),userDto.getPageSize(),true);
        List<UserVo> userVos = userService.selectAllUserByLike(userDto);
        PageInfo<UserVo> pageInfo=new PageInfo<>(userVos);
        return Result.ok().data("userList",userVos).data("total",pageInfo.getTotal());
    }

    @PostMapping("add")
    public Result add(@RequestBody UserDto userDto){
        userService.insertToUser(userDto);
        return Result.ok();
    }
    @GetMapping("{id}")
    public Result updateUser(@PathVariable Long id){
        UserVo user = userService.selectByUid(id);
        return Result.ok().data("before",user);
    }
    @PutMapping("")
    public Result updateUser1(@RequestBody UserDto userDto){
        userService.updateUser(userDto);
        return Result.ok();
    }


    @PostMapping("updatePwd")
    public Result updatePwd(@RequestBody UpdatePwdDto updatePwdDto){
        int i = userService.updatePwd(updatePwdDto);
        return i==1?Result.ok():Result.error();
    }

    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user){
        int i = userService.updateByPrimaryKeySelective(user);
        User user1 = userService.selectByTelOrMail(user.getUserTel());
        return Result.ok().data("user",user1);
    }



    @DeleteMapping("{id}")
    public Result del(@PathVariable Long id){
       int i= userService.deleteByPrimaryKey(id);
        return Result.ok();
    }
    @PutMapping("roles")
    public Result addRole(@RequestBody JSONObject jsonObject){
        int code = 200;
        Long uid=jsonObject.getLong("uid");
        Object rids[]=jsonObject.getJSONArray("roles").toArray();
        try {
            userRoleService.addRole(uid,rids);
        }catch (Exception e){
            code = 500;
        }
        return Result.ok(code,"授权成功");
    }
}

